package com.xxx.controller;

import com.xxx.entity.Books;
import com.xxx.entity.Menu;
import com.xxx.entity.RE;
import com.xxx.service.BookService;
import com.xxx.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
@Api(tags = "菜单操作")
public class MenuController {

    @Autowired
    MenuService menuService;

    @GetMapping("/getById/{id}")
    @ApiOperation("根据ID获取菜单")
    public RE getById(@PathVariable("id") Integer id){
        try {
            Menu menu = menuService.getById(id);
            return new RE().ok(menu);
        }catch (Exception e){
            return RE.error(e.getMessage());
        }
    }

    @GetMapping("/getAll")
    @ApiOperation("获取菜单树")
    public RE getAll(){
        try {
            List<Menu> menuList = menuService.getAll();
            return new RE().ok(menuList);
        }catch (Exception e){
            return RE.error(e.getMessage());
        }
    }

    @PostMapping("/saveMenu")
    @ApiOperation("保存菜单")
    public RE saveMenu(@RequestBody Menu menu){
        try {
            int count = menuService.saveMenu(menu);
            return new RE().ok(count);
        }catch (Exception e){
            return RE.error(e.getMessage());
        }
    }

    @PostMapping("/updateById")
    @ApiOperation("修改菜单")
    public RE updateById(@RequestBody Menu menu){
        try {
            int count = menuService.update(menu);
            return new RE().ok(count);
        }catch (Exception e){
            return RE.error(e.getMessage());
        }
    }

    @PostMapping("/deleteById")
    @ApiOperation("删除菜单，同时删除菜单下的图书")
    public RE deleteById(@RequestBody Integer id){
        try {
            int count = menuService.delete(id);
            return new RE().ok(count);
        }catch (Exception e){
            return RE.error(e.getMessage());
        }
    }
}
